package sql


import (
	"os"
	_ "github.com/lib/pq"

	"gitee.com/lzqysoft/framework/common/log"
	"github.com/go-xorm/xorm"
	"github.com/go-xorm/core"
)


var db *xorm.Engine

func init() {
	log.Info("初始化base模块数据库中")
	connStr := "host=127.0.0.1 user=ilive password=ilive port=5432 dbname=xorm sslmode=disable"
	DB, err := xorm.NewEngine("postgres", connStr)
	if err != nil {
		log.Info("初始化base模块数据库失败")
		log.Error(err)
		os.Exit(1)
	}
	DB.SetLogLevel(core.LOG_DEBUG)
	if err = DB.Ping(); err != nil {
		log.Error(err)
		log.Info("初始化base模块数据库失败")
		os.Exit(1)
	} else {
		log.Info("初始化base模块数据库成功")
	}
	db = DB
}

// GetConn 获取数据库连接
func GetConn()(*xorm.Engine, error) {
	if err := db.Ping(); err != nil {
		return nil, err
	}
	return db, nil
}